package com.gugee.tiktok.data.consumer.music.listener;

import com.gugee.tiktok.data.common.msg.MusicMessage;
import com.gugee.tiktok.data.consumer.music.core.service.MusicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 * @author luolifeng
 * Date  2020/10/04
 */
@Component
public class MusicMessageListener {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    MusicService musicService;

    @KafkaListener(id="${kafka.consumer.group.music.id}",topics = {"${kafka.topics.music.online}"},containerFactory = "factoryMusicAck")
    public void listen(List<MusicMessage> messages, Acknowledgment ack) {
        try {
            musicService.updateMusic(messages);

            //todo 开始你的表演
        }catch (Exception e){
            logger.error("处理音乐数据出错",e);
        }finally {
            ack.acknowledge();
            logger.info("消息确认！");
        }
    }
}
